<template>
    <div>
        <el-card class="!border-none mb-4" shadow="never">
            <el-form class="mb-[-16px]" :model="queryParams" inline>
                <el-form-item label="订单导入时间" prop="orderTime">
                    <daterange-picker v-model:startTime="queryParams.start_time"
                        v-model:endTime="queryParams.end_time" />
                </el-form-item>
                <el-form-item label="商品名称" prop="goodName">
                    <el-input class="w-[280px]" v-model="queryParams.goodName" clearable placeholder="请输入商品名称" />
                </el-form-item>
                <el-form-item label="来源" prop="goodName">
                    <el-select v-model="queryParams.type" placeholder="来源">
                        <el-option label="全部" value=""></el-option>
                        <el-option label="美菜" value="0"></el-option>
                        <el-option label="快驴" value="1"></el-option>
                        <el-option label="优大集" value="3"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="数据展示形式" prop="sumType">
                    <el-select v-model="queryParams.sumType" placeholder="数据展示形式">
                        <el-option label="全部" :value="0"></el-option>
                        <el-option label="合并" :value="1"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="query">查询</el-button>
                    <el-button @click="resetParams">重置</el-button>
                </el-form-item>
            </el-form>
        </el-card>
        <el-card class="!border-none" v-loading="pager.loading" shadow="never">
            <el-row :gutter="20">
                <el-col :span="6">
                    <el-button v-perms="['skuMeicai.sku_meicai/add']" type="primary" @click="handleAdd">
                        <template #icon>
                            <icon name="el-icon-Plus" />
                        </template>
                        新增
                    </el-button>
                    <el-button v-perms="['skuMeicai.sku_meicai/delete']" :disabled="!selectData.length"
                        @click="handleDelete(selectData)">
                        删除
                    </el-button>
                    <el-button type="primary" @click="handleUpload">导入数据</el-button>
                    <el-button type="primary" @click="exportExcel">导出数据</el-button>
                </el-col>
                <el-col :span="18">
                    <el-row :gutter="20">
                        <el-col :span="6"> 查询总数量：{{ total.num }} </el-col>
                        <el-col :span="6"> 查询总金额：{{ total.price }} </el-col>
                    </el-row>
                </el-col>
            </el-row>
            <div class="mt-4">
                <el-table :data="pager.lists" @selection-change="handleSelectionChange">
                    <el-table-column type="selection" width="55" />
                    <el-table-column label="商品编号" prop="number" show-overflow-tooltip />
                    <el-table-column label="商品全名" prop="name" show-overflow-tooltip />
                    <el-table-column label="商品单位" prop="unit" show-overflow-tooltip />
                    <el-table-column label="数量" prop="num" show-overflow-tooltip />
                    <el-table-column label="单价" prop="price" show-overflow-tooltip />
                    <el-table-column label="总价" show-overflow-toolti>
                        <template #default="{ row }">
                            <div>
                                <el-tag type="success" effect="dark">
                                    {{ (row.price * row.num).toFixed(2) }}
                                </el-tag>
                            </div>
                        </template>
                    </el-table-column>
                    <el-table-column label="来源" show-overflow-toolti>
                        <template #default="{ row }">
                            <div>
                                <el-tag v-if="row.type == 0" effect="dark">美菜</el-tag>
                                <el-tag v-if="row.type == 1" type="warning" effect="dark">快驴</el-tag>
                                <el-tag v-if="row.type == 3" type="success" effect="dark">优大集</el-tag>
                            </div>
                        </template>
                    </el-table-column>
                    <el-table-column label="创建时间" prop="create_time">
                        <template #default="{ row }">
                            <span>{{
                row.create_time
                    ? timeFormat(row.create_time, 'yyyy-mm-dd hh:MM:ss')
                    : ''
            }}</span>
                        </template>
                    </el-table-column>
                    <el-table-column label="操作" width="120" fixed="right">
                        <template #default="{ row }">
                            <el-button v-perms="['skuMeicai.sku_meicai/edit']" type="primary" link
                                @click="handleEdit(row)">
                                编辑
                            </el-button>
                            <el-button v-perms="['skuMeicai.sku_meicai/delete']" type="danger" link
                                @click="handleDelete(row.id)">
                                删除
                            </el-button>
                        </template>
                    </el-table-column>
                </el-table>
            </div>
            <div class="flex justify-end mt-4">
                <pagination v-model="pager" @change="getLists" />
            </div>
        </el-card>
        <edit-popup v-if="showEdit" ref="editRef" :dict-data="dictData" @success="getLists" @close="showEdit = false" />
        <UploadPopup v-if="showUpload" ref="uploadRef" @refresh="refresh" @close="showEdit = false" />
    </div>
</template>

<script lang="ts" setup name="skuMeicaiLists">
import { usePaging } from '@/hooks/usePaging'
import { useDictData } from '@/hooks/useDictOptions'
import { apiSkuMeicaiLists, apiSkuMeicaiDelete, getTotal } from '@/api/sku_meicai'
import moment from 'moment'
import { timeFormat } from '@/utils/util'
import feedback from '@/utils/feedback'
import EditPopup from './edit.vue'
import UploadPopup from './sku-upload.vue'
import { AppTypeMap } from '@/enums/skuAPPEnums'
import { Promotion } from '@element-plus/icons-vue'

const editRef = shallowRef<InstanceType<typeof EditPopup>>()
// 是否显示编辑框
const showEdit = ref(false)
// 是否显示上传框
const showUpload = ref(false)
const uploadRef = shallowRef<InstanceType<typeof EditPopup>>()
// 查询条件
const queryParams = reactive({
    orderTime: '',
    goodName: '',
    uniacid: '',
    type: '',
    sumType: 0,
    start_time: moment().startOf('day').format('YYYY-MM-DD HH:mm:ss'),
    end_time: moment().endOf('day').format('YYYY-MM-DD HH:mm:ss')
})

// 统计总数
const total = reactive({
    price: 0,
    num: 0
})

// 选中数据
const selectData = ref<any[]>([])

// 表格选择后回调事件
const handleSelectionChange = (val: any[]) => {
    selectData.value = val.map(({ id }) => id)
}

// 获取字典数据
const { dictData } = useDictData('')

// 分页相关
const { pager, getLists, resetParams, resetPage } = usePaging({
    fetchFun: apiSkuMeicaiLists,
    params: queryParams
})

// 添加
const handleAdd = async () => {
    showEdit.value = true
    await nextTick()
    editRef.value?.open('add')
}
// 刷新数据
const refresh = () => {
    getLists()
    totalNum()
}
// 编辑
const handleEdit = async (data: any) => {
    showEdit.value = true
    await nextTick()
    editRef.value?.open('edit')
    editRef.value?.setFormData(data)
}

// 查询按钮
const query = () => {
    resetPage()
    totalNum()
}

// 下载表格
const exportExcel = () => {
    const data = {
        export: 2,
        start_time: queryParams.start_time,
        end_time: queryParams.end_time,
        type: queryParams.type,
        goodName: queryParams.goodName,
        page_type: 0,
        sumType: queryParams.sumType
    }
    apiSkuMeicaiLists(data).then((res) => {
        console.log(res)
    })
}

// 删除
const handleDelete = async (id: number | any[]) => {
    await feedback.confirm('确定要删除？')
    await apiSkuMeicaiDelete({ id })
    getLists()
    totalNum()
}
const handleUpload = async (data: any) => {
    showUpload.value = true
    await nextTick()
    uploadRef.value?.open('edit')
}
// 获取统计数据
const totalNum = () => {
    const data = queryParams
    getTotal(data).then((res) => {
        total.num = res.num
        total.price = res.price
    })
}
getLists()
totalNum()
</script>
